-module(emqx_plugin_rabbitmq_schema).

-include_lib("hocon/include/hoconsc.hrl").
-include_lib("emqx/include/logger.hrl").

-export([roots/0, fields/1, desc/1]).
-import(hoconsc, [enum/1]).

roots() -> [plugin_rabbitmq].

desc(_) -> undefined.

fields(plugin_rabbitmq) ->
    [
        {connection, ?HOCON(?R_REF(connection), #{desc => ?DESC("connection")})},
        {producer, ?HOCON(?R_REF(producer), #{desc => ?DESC("producer")})},
        {topics, ?HOCON(?ARRAY(?R_REF(topics)),
            #{
                required => true,
                default => [],
                desc => ?DESC("topics")
            })}
    ];

fields(connection) ->
    [
        {hosts, ?HOCON(string(),
            #{
                required => true,
                default => "127.0.0.1:5672",
                desc => ?DESC("hosts"),
                validator => emqx_schema:servers_validator(#{default_port => 5672}, true)
            })},
        {username, ?HOCON(string(), #{default => <<"guest">>, desc => ?DESC("username")})},
        {password, emqx_schema_secret:mk(#{default => <<"guest">>, desc => ?DESC("password")})},
        {virtual_host, ?HOCON(string(), #{default => <<"/">>, desc => ?DESC("virtual_host")})},
        {heartbeat, ?HOCON(emqx_schema:timeout_duration(), #{default => <<"60s">>, desc => ?DESC("heartbeat")})}
    ];

fields(host) ->
    [
        {host, ?HOCON(string(),
            #{
                validator => emqx_schema:servers_validator(#{default_port => 5672}, true)
            })}
    ];

fields(producer) ->
    [
        {send_buffer, ?HOCON(emqx_schema:bytesize(), #{default => "1024KB", desc => ?DESC("send_buffer")})},
        {pool_size, ?HOCON(integer(), #{default => 8, desc => ?DESC("pool_size")})},
        {reconnect_interval, ?HOCON(emqx_schema:timeout_duration(), #{default => <<"3s">>, desc => ?DESC("reconnect_interval")})},
        {default_exchange, ?HOCON(binary(), #{default => <<"">>, desc => ?DESC("default_exchange")})},
        {default_exchange_type, ?HOCON(binary(), #{default => <<"fanout">>, desc => ?DESC("default_exchange_type")})},
        {default_routing_key, ?HOCON(binary(), #{default => <<"emqx_message">>, desc => ?DESC("default_routing_key")})},
        {persistent_messages, ?HOCON(boolean(), #{default => true, desc => ?DESC("persistent_messages")})}
    ];

fields(topics) ->
    [
        {filter, ?HOCON(binary(), #{desc => ?DESC("topics_filter"), default => <<"#">>})},
        {exchange, ?HOCON(binary(), #{desc => ?DESC("topics_exchange"), default => <<"amq.topic">>})},
        {exchange_type, ?HOCON(enum(['direct', 'topic', 'fanout', 'headers']),
                              #{default => 'fanout', desc => ?DESC("topics_exchange_type")})},
        {routing_key, ?HOCON(binary(), #{default => <<"emqx.mqtt">>, desc => ?DESC("routing_key")})},
        {rabbitmq_message, ?HOCON(binary(), #{desc => ?DESC("topics_rabbitmq_message"), default => <<"${.}">>})}
    ].

%% 构建 hosts 字段的 schema，用于解析逗号分隔的 host:port 列表
%% hosts() ->
%%     Meta = #{desc => ?DESC("hosts")},
%%     emqx_schema:servers_sc(Meta, #{default_port => 5672}).
